package com.cn.medicalcare.controller.backstageservlet.news;

import com.cn.medicalcare.customtool.JsonTool;
import com.cn.medicalcare.pojo.News;
import com.cn.medicalcare.pojo.NewsInfo;
import com.cn.medicalcare.service.NewsInfoService;
import com.cn.medicalcare.service.NewsService;
import com.cn.medicalcare.service.serviceimpl.NewsInfoServiceImpl;
import com.cn.medicalcare.service.serviceimpl.NewsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.LinkedHashMap;
import java.util.Map;

@WebServlet("/backstage/news/updatenewsinfo.json")
public class NewsInfoUpdateServlet extends HttpServlet {

    private NewsInfoService newsInfoService = NewsInfoServiceImpl.getInstance();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String news_id = req.getParameter("news_id");
        String source = req.getParameter("source");
        String author = req.getParameter("author");
        String content = req.getParameter("content");
        String numviews = req.getParameter("numviews");
        String survival = req.getParameter("survival");

        NewsInfo newsInfo = new NewsInfo();
        Map<String, Object> map = new LinkedHashMap<>();
        try {
            newsInfo.setNews_id(Integer.valueOf(news_id))
                    .setNumviews(Integer.valueOf(numviews)).setSurvival(Integer.valueOf(survival))
                    .setSource(source).setAuthor(author).setContent(content);
            if (newsInfoService.updateNewsInfo(newsInfo)) {
                map.put("status", 200);
            } else {
                map.put("status", 500);
            }
        } catch (Exception e) {
            map.put("status", 400);
        }
        resp.getWriter().print(JsonTool.toJson(map));
    }
}
